package es.castorm.arch.data.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.PriorityOrdered;

/**
 * The Class JpaPostProcessor.
 */
public class EntityManagerFactoryPostProcessor implements BeanPostProcessor, PriorityOrdered {

	/** The packages to scan. */
	private String packagesToScan;

	/**
	 * Instantiates a new jpa post processor.
	 * 
	 * @param packagesToScan the packages to scan
	 */
	public EntityManagerFactoryPostProcessor(String packagesToScan) {
		super();
		this.packagesToScan = packagesToScan;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

		if (bean instanceof EntityManagerFactoryWrapper) {
			((EntityManagerFactoryWrapper) bean).unwrap().setPackagesToScan(packagesToScan);
		}

		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

		return bean;
	}

	@Override
	public int getOrder() {

		return 0;
	}
}
